package org.laizili.solution.nowcodertop101.bitree;

import org.laizili.structure.tree.bitree.TreeNode;

/**
 * <a href="https://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b?tpId=295&sfm=html&channel=nowcoder">BM34. 判断是不是二叉搜索树</a>
 * <p>tags: 二叉搜索树定义; 二叉树遍历</p>
 *
 */
public class BM34 {
    private static class Solution {
        private TreeNode preNode = null;

        public boolean isValidBST(TreeNode root) {
            if (root == null) {
                return true;
            }
            if (!isValidBST(root.left)) {
                return false;
            }
            if (preNode != null && root.val < preNode.val) {
                return false;
            }
            preNode = root;
            return isValidBST(root.right);
        }
    }
}
